<?php
namespace app\common\helper\util;

define('NOW_TIME', time());
define('TODAY_TIME', strtotime(date('Y-m-d', NOW_TIME)));
define('YESTERDAY_TIME', TODAY_TIME - 86400);
$now_week                   = date('w', NOW_TIME);
$now_week == 0 && $now_week = 7;
define('NOW_WEEK_TIME', TODAY_TIME - ($now_week - 1) * 86400);
define('NOW_YEAR_TIME', strtotime(date('Y-01-01')));

class TimeFormat
{
    public $time; //要计算的时间的时间戳
    const now_time       = NOW_TIME;
    const today_time     = TODAY_TIME;
    const yesterday_time = YESTERDAY_TIME;
    const now_week_time  = NOW_WEEK_TIME;
    const now_year_time  = NOW_YEAR_TIME;

    public function __construct($time = null)
    {
        !empty($time) && $this->time = $time;
        $this->time                  = $time;
    }

    /**
     * 返回 上午，下午，晚上，凌晨
     * @param null $time
     * @return string
     */
    public function get_pre_name($time = null)
    {
        !empty($time) && $this->time = $time;
        $the_day_time                = strtotime(date('Y-m-d', $this->time));
        $one_day_second              = $this->time - $the_day_time;
        if ($one_day_second < 3600 * 6) {
            $pre_name = '凌晨';
        } elseif ($one_day_second < 3600 * 12) {
            $pre_name = '上午';
        } elseif ($one_day_second < 3600 * 18) {
            $pre_name = '下午';
        } else {
            $pre_name = '晚上';
        }
        return $pre_name;
    }


    public function get_week_name($time = null)
    {
        !empty($time) && $this->time = $time;
        $the_week                    = date('w', $this->time);
        $the_week == 0 && $the_week  = 7;
        $week_pre_name               = '';
        switch ($the_week) {
            case 1:
                $week_pre_name = '一';
                break;
            case 2:
                $week_pre_name = '二';
                break;
            case 3:
                $week_pre_name = '三';
                break;
            case 4:
                $week_pre_name = '四';
                break;
            case 5:
                $week_pre_name = '五';
                break;
            case 6:
                $week_pre_name = '六';
                break;
            case 7:
                $week_pre_name = '日';
                break;
            default:
                $week_pre_name = '';
        }
        return $week_pre_name;
    }


    public function formate($time = null)
    {
        !empty($time) && $this->time = $time;
        $test                        = '';
        if ($this->time >= self::now_time) {
            //未来
            $test = '';
        } elseif ($this->time > self::today_time) {
            //今天
            $diff_time = self::now_time - $this->time;
            if ($diff_time < 60) {
                $test = $diff_time . '秒前';
            } elseif ($diff_time < 3600) {
                $test = intval($diff_time / 60) . '分钟前';
            } else {
                $test = intval($diff_time / 3600) . '小时前';
            }
        } elseif ($this->time > self::yesterday_time) {
            //昨天
            $pre_name = $this->get_pre_name();
            $test     = '昨天 ' . $pre_name . ' ' . date('H:i', $this->time);
        } elseif ($this->time > self::now_week_time) {
            //这周内
            $pre_name      = $this->get_pre_name();
            $week_pre_name = $this->get_week_name();
            $test          = '周' . $week_pre_name . ' ' . $pre_name . ' ' . date('H:i', $this->time);
        } elseif ($this->time > self::now_year_time) {
            //今年
            $pre_name = $this->get_pre_name();
            $test     = date('m月d日', $this->time) . ' ' . $pre_name . ' ' . date('H:i', $this->time);
        } else {
            //更早
            $pre_name = $this->get_pre_name();
            $test     = date('Y月m月d日', $this->time) . ' ' . $pre_name . ' ' . date('H:i', $this->time);
        }
        return $test;
    }
}
